<?php

/**
 * business actions.
 *
 * @package    sf_sandbox
 * @subpackage business
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 2692 2006-11-15 21:03:55Z fabien $
 */
class businessActions extends sfActions
{

    public function executeInitData()
    {
        //$this->doUpdateActiveDatetime();
        //$this->revalidateByParingLedger(null);
        $this->doUpgradePackage(1314, 470);
        print_r("Done");
        return sfView::HEADER_ONLY;
    }
    function doUpgradePackage($distId, $pairingPoint) {
        $distDB = MlmDistributorPeer::retrieveByPk($distId);
        $sponsorDB = MlmDistributorPeer::retrieveByPk($distDB->getParentId());

        $distAccountEcashBalance = $this->getAccountBalance($distId, Globals::ACCOUNT_TYPE_ECASH);

        $mlm_account_ledger = new MlmAccountLedger();
        $mlm_account_ledger->setDistId($distDB->getParentId());
        $mlm_account_ledger->setAccountType(Globals::ACCOUNT_TYPE_ECASH);
        $mlm_account_ledger->setTransactionType(Globals::ACCOUNT_LEDGER_ACTION_DSB);
        $mlm_account_ledger->setRemark("DIRECT SPONSOR BONUS AMOUNT (" . $distDB->getDistributorCode() . ")");
        $mlm_account_ledger->setCredit($pairingPoint);
        $mlm_account_ledger->setDebit(0);
        $mlm_account_ledger->setBalance($distAccountEcashBalance + $pairingPoint);
        $mlm_account_ledger->setCreatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID, Globals::SYSTEM_USER_ID));
        $mlm_account_ledger->setUpdatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID, Globals::SYSTEM_USER_ID));
        $mlm_account_ledger->save();

        $maintenanceBalance = $pairingPoint * 0.3;

        $maintenanceEcashAccountLedger = new MlmAccountLedger();
        $maintenanceEcashAccountLedger->setDistId($distDB->getParentId());
        $maintenanceEcashAccountLedger->setAccountType(Globals::ACCOUNT_TYPE_ECASH);
        $maintenanceEcashAccountLedger->setTransactionType(Globals::ACCOUNT_LEDGER_ACTION_MAINTENANCE);
        $maintenanceEcashAccountLedger->setRemark("MAINTENANCE BALANCE (" . $distDB->getDistributorCode() . ")");
        $maintenanceEcashAccountLedger->setCredit(0);
        $maintenanceEcashAccountLedger->setDebit($maintenanceBalance);
        $maintenanceEcashAccountLedger->setBalance($distAccountEcashBalance + ($pairingPoint * 0.7) - $maintenanceBalance);
        $maintenanceEcashAccountLedger->setCreatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID, Globals::SYSTEM_USER_ID));
        $maintenanceEcashAccountLedger->setUpdatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID, Globals::SYSTEM_USER_ID));
        $maintenanceEcashAccountLedger->save();

        $this->revalidateAccount($distDB->getParentId(), Globals::ACCOUNT_TYPE_ECASH);

        /******************************/
        /*  Maintenance
        /******************************/
        $distAccountMaintenanceBalance = $this->getAccountBalance($distId, Globals::ACCOUNT_TYPE_MAINTENANCE);

        $maintenanceAccountLedger = new MlmAccountLedger();
        $maintenanceAccountLedger->setDistId($distDB->getParentId());
        $maintenanceAccountLedger->setAccountType(Globals::ACCOUNT_TYPE_MAINTENANCE);
        $maintenanceAccountLedger->setTransactionType(Globals::ACCOUNT_LEDGER_ACTION_DSB);
        $maintenanceAccountLedger->setRemark("MAINTENANCE BALANCE (" . $distDB->getDistributorCode() . ")");
        $maintenanceAccountLedger->setCredit($maintenanceBalance);
        $maintenanceAccountLedger->setDebit(0);
        $maintenanceAccountLedger->setBalance($distAccountMaintenanceBalance + $maintenanceBalance);
        $maintenanceAccountLedger->setCreatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID, Globals::SYSTEM_USER_ID));
        $maintenanceAccountLedger->setUpdatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID, Globals::SYSTEM_USER_ID));
        $maintenanceAccountLedger->save();

        $this->revalidateAccount($distDB->getParentId(), Globals::ACCOUNT_TYPE_MAINTENANCE);



        $uplineDistDB = MlmDistributorPeer::retrieveByPk($distDB->getUplineDistId());
        $uplinePosition = $distDB->getPlacementPosition();

        $arrs = explode("|", $distDB->getTreeStructure());
        for ($x = count($arrs); $x > 0; $x--) {
            if ($arrs[$x] == "" || $distDB->getDistributorCode() == $arrs[$x]) {
                continue;
            }
            $legBalance = $this->getPairingBalance($uplineDistDB->getDistributorId(), $uplinePosition);

            $sponsorDistPairingledger = new MlmDistPairingLedger();
            $sponsorDistPairingledger->setDistId($uplineDistDB->getDistributorId());
            $sponsorDistPairingledger->setLeftRight($uplinePosition);
            $sponsorDistPairingledger->setTransactionType(Globals::PAIRING_LEDGER_REGISTER);
            $sponsorDistPairingledger->setCredit($pairingPoint);
            $sponsorDistPairingledger->setDebit(0);
            $sponsorDistPairingledger->setBalance($legBalance + $pairingPoint);
            $sponsorDistPairingledger->setRemark("PAIRING POINT AMOUNT (" . $distDB->getDistributorCode() . ")");
            $sponsorDistPairingledger->setCreatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID, Globals::SYSTEM_USER_ID));
            $sponsorDistPairingledger->setUpdatedBy($this->getUser()->getAttribute(Globals::SESSION_USERID, Globals::SYSTEM_USER_ID));
            $sponsorDistPairingledger->save();

            $this->revalidatePairing($uplineDistDB->getDistributorId(), $uplinePosition);

            $uplinePosition = $uplineDistDB->getPlacementPosition();
            $uplineDistDB = MlmDistributorPeer::retrieveByPk($uplineDistDB->getUplineDistId());
        }
    }

    function doUpdateActiveDatetime() {
        $c = new Criteria();
        $c->add(MlmDistributorPeer::TREE_STRUCTURE, '%61181122%', Criteria::LIKE);
        $distributors = MlmDistributorPeer::doSelect($c);

        $date = null;
        $i = 0;
        $dateUtil = new DateUtil();
        foreach ($distributors as $distributor) {
            $i++;
            print_r("DIST CODE" . $distributor->getDistributorCode());
            print_r("<br>");
            print_r("ACTIVE DATE" . $distributor->getActiveDatetime());
            print_r("<br>");

            if ($i == 1 || $distributor->getDistributorCode() == '97429673') {
                $date = $distributor->getActiveDatetime();
                continue;
            }
            $date = $dateUtil->addDate($date, 1, 0, 0);
            $distributor->setActiveDatetime($date);
            $distributor->save();
            print_r("AFTER ACTIVE DATE" . $distributor->getActiveDatetime());
            print_r("<br>");
        }
    }
    function revalidateByParingLedger($distId)
    {
        $c = new Criteria();
        if ($distId != null) {
            $c->add(MlmDistributorPeer::DISTRIBUTOR_ID, $distId);
        }
        $distributors = MlmDistributorPeer::doSelect($c);
        foreach ($distributors as $distributor) {
            print_r("DIST ID" . $distributor->getDistributorId());
            print_r("<br>");

            $this->revalidatePairing($distributor->getDistributorId(), Globals::PLACEMENT_LEFT);
            $this->revalidatePairing($distributor->getDistributorId(), Globals::PLACEMENT_RIGHT);
        }
    }

    function revalidatePairing($distributorId, $leftRight)
    {
        $balance = $this->getPairingBalance($distributorId, $leftRight);

        $c = new Criteria();
        $c->add(MlmDistPairingPeer::DIST_ID, $distributorId);
        $tbl_account = MlmDistPairingPeer::doSelectOne($c);

        if (!$tbl_account) {
            $tbl_account = new MlmDistPairing();
            $tbl_account->setDistId($distributorId);
            $tbl_account->setLeftBalance(0);
            $tbl_account->setRightBalance(0);
        }
        if (Globals::PLACEMENT_LEFT == $leftRight) {
            $tbl_account->setLeftBalance($balance);
        } else if (Globals::PLACEMENT_RIGHT == $leftRight) {
            $tbl_account->setRightBalance($balance);
        }

        $tbl_account->save();
    }

    function getPairingBalance($distributorId, $leftRight)
    {
        $query = "SELECT SUM(credit-debit) AS SUB_TOTAL FROM mlm_dist_pairing_ledger WHERE dist_id = " . $distributorId . " AND left_right = '" . $leftRight . "'";

        $connection = Propel::getConnection();
        $statement = $connection->prepareStatement($query);
        $resultset = $statement->executeQuery();

        $count = 0;
        if ($resultset->next()) {
            $arr = $resultset->getRow();
            if ($arr["SUB_TOTAL"] != null) {
                return $arr["SUB_TOTAL"];
            } else {
                return 0;
            }
        }
        return 0;
    }

    function revalidateAccount($distributorId, $accountType)
    {
        $balance = $this->getAccountBalance($distributorId, $accountType);

        $c = new Criteria();
        $c->add(MlmAccountPeer::ACCOUNT_TYPE, $accountType);
        $c->add(MlmAccountPeer::DIST_ID, $distributorId);
        $tbl_account = MlmAccountPeer::doSelectOne($c);

        if (!$tbl_account) {
            $tbl_account = new MlmAccount();
            $tbl_account->setDistId($distributorId);
            $tbl_account->setAccountType($accountType);
        }

        $tbl_account->setBalance($balance);
        $tbl_account->save();
    }

    function getAccountBalance($distributorId, $accountType)
    {
        $query = "SELECT SUM(credit-debit) AS SUB_TOTAL FROM mlm_account_ledger WHERE dist_id = " . $distributorId . " AND account_type = '" . $accountType . "'";

        $connection = Propel::getConnection();
        $statement = $connection->prepareStatement($query);
        $resultset = $statement->executeQuery();

        if ($resultset->next()) {
            $arr = $resultset->getRow();
            if ($arr["SUB_TOTAL"] != null) {
                return $arr["SUB_TOTAL"];
            } else {
                return 0;
            }
        }
        return 0;
    }
}
